package de.wittje.games.mlpbd.view.panels;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import de.wittje.games.mlpbd.controller.ExternalizingController;
import de.wittje.games.mlpbd.view.windows.MainWindow;

public class OptionPanel extends JPanel {

	private final String BACKGROUNDPFAD = "/de/wittje/games/mlpbd/data/images/titleBackground.png";

	private ExternalizingController ec;
	private MainWindow window;
	private ArrayList<String> options;
	private int selectedOption = 1;

	public OptionPanel(ExternalizingController ec, MainWindow window) {

		this.ec = ec;
		this.window = window;
		options = new ArrayList<String>();
		options.add(ec.getResource("languageOption"));
		options.add(ec.getResource("solution"));
		repaint();

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		drawBackground(g);
		drawOptions(g);

	}

	private void drawBackground(Graphics g) {
		BufferedImage img;
		try {
			img = ImageIO.read(this.getClass().getResourceAsStream(
					BACKGROUNDPFAD));
			int x = (getWidth() - img.getWidth()) / 2;
			int y = getHeight() - img.getHeight();
			g.drawImage(img, x, y, null, null);

			g.setColor(new Color(0, 0, 0, 128));
			g.fillRect(0, 0, getWidth(), getHeight());

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void drawOptions(Graphics g) {

		try {
			InputStream is = this.getClass().getResourceAsStream(
					"/de/wittje/games/mlpbd/data/Equestria.ttf");
			Font f;
			f = Font.createFont(Font.TRUETYPE_FONT, is);
			f = f.deriveFont(50f);
			g.setFont(f);

			g.setColor(new Color(255, 153, 27));

			FontMetrics fm = g.getFontMetrics();

			int i = 1;

			for (String s : options) {

				if (i == selectedOption) {
					g.setColor(new Color(240, 177, 213));
				} else {
					g.setColor(new Color(255, 153, 27));
				}

				int x = 15;
				int y = 0 + fm.getHeight() * i + 5 * i + 10;

				g.drawString(s, x, y);
				i++;
			}

		} catch (FontFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
